package icantalk;

import java.io.File;


import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;

/**
 * This class makes the sounds go on.
 * @author Owner
 *
 */
public class Sounds implements Runnable {
	
	private Clip clip;
	private File soundFile;
	private int _sleep;
	
	/**
	 * A constructor
	 * @param soundnumber The chosen sound: 1 for "welcome" sound.
	 *  2 for "massage" sound.
	 *  3 for "quit" sound. 
	 */
	public Sounds(int soundnumber) {
		switch (soundnumber)
		{
		case 1:soundFile = new File("src/sounds/welcome.wav"); break;
		case 2:soundFile = new File("src/sounds/hello.wav"); break;
		case 3:soundFile = new File("src/sounds/quit.wav"); _sleep = 3000; break;
		default: _sleep = 0;
		}
		this.run();	    
	}
	/**
	 * This method plays the sound
	 * @throws Exception If a sound error. 
	 */
	public void playsound() throws Exception  
	{
		AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);
	    DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
	    clip = (Clip) AudioSystem.getLine(info);
	    clip.open(sound);
	    clip.addLineListener(new LineListener() {
	      public void update(LineEvent event) {
	        if (event.getType() == LineEvent.Type.STOP) {
	          event.getLine().close();
	          
	        }
	      }
	    });
	    
		clip.start();
		Thread.sleep(_sleep);
		
		
	}
	@Override
	/**
	 * Runs playsound method 
	 */
	public void run() {
		try {
			this.playsound();
		} catch (Exception e) {

			e.printStackTrace();
		}
		
	}

}
